package com.feizi.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.feizi.entity.UserEntity;
import com.feizi.entity.UserSelectEntity;
import com.feizi.service.UserService;



@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	/**
	 * 全查和分页查询
	 * @param model
	 * @param pse
	 * @return
	 */
	
	@RequestMapping(path="/findUser.do",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> showUsers(UserSelectEntity pse){
		//设置mysql分页的偏移量
		pse.setOffset((pse.getPage()-1)*pse.getLimit());
		
		//查询条数
		int count=userService.getCount(pse);
		
		//全查
		List<UserEntity> list=userService.find(pse);
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", "0");
		rslMap.put("msg", "success");
		rslMap.put("count", count); 
		rslMap.put("data", list);
		return rslMap;
	}
	/**
	 * 修改
	 * @param user
	 * @return
	 */
	@RequestMapping(path="/upUser.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addUser(int userid,String lock,String admin){
		int administrator="on".equals(admin)?1:0;
		int isLock="on".equals(lock)?1:0;
		UserEntity user=new UserEntity();
		user.setUserid(userid);
		user.setAdministrator(administrator);
		user.setIsLock(isLock);
		//受影响的行数
		int rows=userService.upUser(user);
		Map<String, Object> rslMap = new HashMap<String, Object>();
		rslMap.put("code", rows>0? "1" :"0");
		rslMap.put("msg",  rows>0? "修改成功":"修改失败");
		return rslMap;
	}
}
